package net.akkz.dao.impl;

import java.util.ArrayList;

import net.akkz.dao.QuestionDao;
import net.akkz.dbFactory.Session;
import net.akkz.exception.BuildClassError;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.Question;

public class QuestionDaoImpl implements QuestionDao
{

	@Override
	public void addQuestion(Session session, Question question) throws SQLExcuteException
	{
		String sql = "insert into question(question, studentid, ansnum, kind) values(?, ?, ?, ?)";

		Object[] values = new Object[4];
		values[0] = question.getQuestion();
		values[1] = question.getStudentid();
		values[2] = question.getAnsnum();
		values[3] = question.getKind();

		session.excute(sql, values);
	}

	@Override
	public ArrayList<Question> findQuestion(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question";

		return session.find(sql, new Object[0], Question.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Question> findQuestionOrderByGoodNum(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question order by ansnum";

		return session.find(sql, new Object[0], Question.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Question> findQuestionWithKind(Session session, int start, int end, int kind) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question where kind=?";

		return session.find(sql, new Object[]{ kind }, Question.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Question> findQuestionOrderByGoodNumWithKind(Session session, int start, int end, int kind) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question where kind=? order by ansnum";

		return session.find(sql, new Object[]{ kind }, Question.class, start, end - start + 1);
	}

	@Override
	public ArrayList<Question> findQuestionByStudentId(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question where studentid = ?";

		Object[] values = new Object[1];
		values[0] = studentId;

		return session.find(sql, values, Question.class);
	}

	@Override
	public Question getQuestion(Session session, int id) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from question where id = ?";

		return session.get(sql, id, Question.class);
	}

	@Override
	public void deleteQuestion(Session session, int id) throws SQLExcuteException
	{
		String sql = "delete from question where id = ?";

		Object[] values = new Object[1];
		values[0] = id;

		session.excute(sql, values);
	}

}
